package net.ainom.scarab.xmlrpc;

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;

public class ScarabSVNPostCommitCommunicationFilter implements Filter {

    public FilterResponse filter(FilterResponse sb, Object o) throws Exception {
        if (o instanceof SVNLogEntry) {
            SVNLogEntry logEntry = (SVNLogEntry) o;
            String x = logEntry.getMessage();
            if (x == null)
                x = "";
            String xx = null;
            // Determine how to find the Issue identifier. For now, it's the first "word" in the comment
            int jj = x.indexOf(" ");
            if (jj > 0) {
                xx = x.substring(0, jj);
            } else
                xx = x;
            sb.addIssue(xx.trim());

            sb.append(x).append("\n"); // Append the message. We need to do something here to check for the id,
            // I think
            sb.append("---------------------------------------------").append("\n");
            sb.append("revision: " + logEntry.getRevision()).append("\n");
            sb.append("author: " + logEntry.getAuthor()).append("\n");
            sb.append("date: " + logEntry.getDate()).append("\n");
            /*
             * displaying all paths that were changed in that revision; cahnged path information is represented by SVNLogEntryPath.
             */
            if (logEntry.getChangedPaths().size() > 0) {
                sb.append("changed paths:").append("\n");
                Set changedPathsSet = logEntry.getChangedPaths().keySet();

                for (Iterator changedPaths = changedPathsSet.iterator(); changedPaths.hasNext();) {
                    SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry.getChangedPaths().get(changedPaths.next());
                    /*
                     * SVNLogEntryPath.getPath returns the changed path itself;
                     * 
                     * SVNLogEntryPath.getType returns a charecter describing how the path was changed ('A' - added, 'D' - deleted
                     * or 'M' - modified);
                     * 
                     * If the path was copied from another one (branched) then SVNLogEntryPath.getCopyPath &
                     * SVNLogEntryPath.getCopyRevision tells where it was copied from and what revision the origin path was at.
                     */
                    sb.append(
                            " "
                                    + entryPath.getType()
                                    + " "
                                    + entryPath.getPath()
                                    + ((entryPath.getCopyPath() != null) ? " (from " + entryPath.getCopyPath() + " revision "
                                            + entryPath.getCopyRevision() + ")" : "")).append("\n");
                }
            }
            sb.setResponse(1);
        } else
            sb.setResponse(0);
        return sb;

    }

    public void setFilterProperties(Properties p) throws Exception {
        // @TODO We COULD get all the information we need from the properties rather than the FilterResponse object.
        // Currently do nothing
    }

}
